package com.skyk.config;

import com.skyk.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/*创建一个配置类来配置 RedisTemplate*/

@Configuration
@Slf4j
public class RedisConfig {

    // 这里的key不建议写死User，如果以后要缓存其他东西，你是不是又得写多一个？写通用一点，value建议用 Object
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new CustomRedisSerializer<>(User.class)); // 注意：这里可能需要更具体的类型，或者使用泛型擦除和类型转换
        return redisTemplate;
    }
}